package kr.saeildamil.androidstudy_second.gesture;

import java.util.ArrayList;

import kr.saeildamil.androidstudy_second.R;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class GestureOverlay extends Activity {
	WebView mWeb;
	GestureLibrary mLibrary;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_gestureoverlay);
		
		mWeb = (WebView) findViewById(R.id.web);
		mWeb.setWebViewClient(new MyWebClient());
		WebSettings set = mWeb.getSettings();
		set.setJavaScriptEnabled(true);
		set.setBuiltInZoomControls(true);
		mWeb.loadUrl("http://m.naver.com");
		
		mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
		if (!mLibrary.load()){
			finish();
		}
		
		GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
		gestures.addOnGesturePerformedListener(mListener);
	}
	
	class MyWebClient extends WebViewClient{
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return super.shouldOverrideUrlLoading(view, url);
		}
	}
	
	OnGesturePerformedListener mListener = new OnGesturePerformedListener() {
		public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
			ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
			
			if (predictions.size()!= 0){
				Prediction prediction = predictions.get(0);
				String name = prediction.name;
				if (prediction.score > 1.0){
					if (name.equalsIgnoreCase("kiyuk")){
						mWeb.goBack();
					}else if (name.equalsIgnoreCase("niun")){
						mWeb.goForward();
					}else if (name.equalsIgnoreCase("digut")){
						mWeb.loadUrl("http://www.google.co.kr");
					}
				}
			}
		}
	};
}

